package com.ruoyi.project.mahjong.controller;

import java.util.List;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.mahjong.domain.TStaffWork;
import com.ruoyi.project.mahjong.service.ITStaffWorkService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 员工待办Controller
 *
 * @author ruoyi
 * @date 2023-07-18
 */
@RestController
@RequestMapping("/mahjong/work")
public class TStaffWorkController extends BaseController
{
    @Autowired
    private ITStaffWorkService tStaffWorkService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private RedisCache redisCache;
    /**
     * 查询员工待办列表
     */
    @PreAuthorize("@ss.hasPermi('mahjong:work:list')")
    @GetMapping("/list")
    public TableDataInfo list(TStaffWork tStaffWork)
    {
        startPage();
        List<TStaffWork> list = tStaffWorkService.selectTStaffWorkList(tStaffWork);
        return getDataTable(list);
    }

    /**
     * 导出员工待办列表
     */
    @PreAuthorize("@ss.hasPermi('mahjong:work:export')")
    @Log(title = "员工待办", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TStaffWork tStaffWork)
    {
        List<TStaffWork> list = tStaffWorkService.selectTStaffWorkList(tStaffWork);
        ExcelUtil<TStaffWork> util = new ExcelUtil<TStaffWork>(TStaffWork.class);
        return util.exportExcel(list, "work");
    }

    /**
     * 获取员工待办详细信息
     */
//    @PreAuthorize("@ss.hasPermi('mahjong:work:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tStaffWorkService.selectTStaffWorkById(id));
    }

    /**
     * 新增员工待办
     */
    @PreAuthorize("@ss.hasPermi('mahjong:work:add')")
    @Log(title = "员工待办", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TStaffWork tStaffWork)
    {
        return toAjax(tStaffWorkService.insertTStaffWork(tStaffWork));
    }

    /**
     * 修改员工待办
     */
    @PreAuthorize("@ss.hasPermi('mahjong:work:edit')")
    @Log(title = "员工待办", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TStaffWork tStaffWork)
    {
        return toAjax(tStaffWorkService.updateTStaffWork(tStaffWork));
    }

    /**
     * 根据角色code获取人员列表
     * @param code
     * @return
     */
    @GetMapping("/listByRoleCode/{code}")
    public AjaxResult list(@PathVariable String code)
    {
        SysUser user1 = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user1.getUserId());
        List<SysUser> list = userService.selectUserListByRoleKey(code,shopId);
        for(int i=0;i<list.size();i++){
            SysUser user=list.get(i);
            if("2".equals(user.getDelFlag())){
                list.remove(i);
                i--;
            }
        }
        return AjaxResult.success(list);
    }
    /**
     * 删除员工待办
     */
    @PreAuthorize("@ss.hasPermi('mahjong:work:remove')")
    @Log(title = "员工待办", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tStaffWorkService.deleteTStaffWorkByIds(ids));
    }
}
